<?php

namespace controller;

/**
 * Kontroler umożliwiający dodanie sprzedawcy sklepu
 *
 * @author Damian
 */
class ShopAdmin extends Controller{
    /**
     *
     * @var \model\TblSeller <p>sprzedawca</p>
     */
     protected $seller;
    public function __construct($options = array()) {
        parent::__construct($options);
        $session = \library\Registry::get("session");
        $this->seller=$session->get("seller");
       if ($this->person==null) {
           $URL= \library\RequestMethods::server("HTTP_HOST").\library\RequestMethods::server("REDIRECT_URL");
           throw new exceptions\ForbiddenAccessException("dostęp do aderesu {$URL} wymaga zalogwania jako sprzedawca");
       }
       else if ($this->seller==null){
           $URL= \library\RequestMethods::server("HTTP_HOST").\library\RequestMethods::server("REDIRECT_URL");
           throw new exceptions\ForbiddenAccessException("dostęp do aderesu {$URL} wymaga zalogwania jako sprzedawca");
       }
       else {
           $view = $this->getActionView();
           $view->set("CreatedShop",  $this->seller->CreatedShop);
       }
   }
    public function index(){
        $name = \library\RequestMethods::post("inputShopName",null);
        $description = \library\RequestMethods::post("ShopRegister",null);
        if ($name!=null && $description!=null){
            $shop = new \model\TblShop([            
                    \model\TblShop::DESCRIPTION=>$description,
                    \model\TblShop::NAME=>$name
                   // \model\TblShop::SELLER_ID => (int)$this->seller->SellerID
                    ]);
                    
            if ($shop->validate()){
                try{
                    
                    $shopID=$shop->save();
                    $shopSeller = new \model\TblShopSeller([
                    \model\TblShopSeller::SELLER_ID =>$this->seller->SellerID,
                    \model\TblShopSeller::SHOP_ID =>$shopID                            
                    ]);
                    $this->seller->CreatedShop=1;
                    $this->seller->save();
                    $shopSeller->save();
                    $category = new \model\TblCategory([
                    \model\TblCategory::SHOP_ID=>$shopID,
                    \model\TblCategory::NAME=>"Kategoria Główna",
                    \model\TblCategory::DESCRIPTION =>"Kategoria Głowna",
                    \model\TblCategory::DEPTH=>0,
                    \model\TblCategory::HAS_CHILDREN=>0
                    ]);
                    $category->generateURL();
                    $category->save();
                    $this->redirect($shop->URL."/seller");
                }
                catch (\model\exceptions\ExceptionSQLQUery $ex){
                    if ($ex->getCode()==1062){
                        $errors = array();
                        $errors["Name"][]="Sklep o takiej nazwie już istnieje";
                        $view = $this->getActionView();
                        $view->set("errors",$errors);
                        $this->render();
                        
                    }
                    else {
                        throw $ex;
                    }
                }
                finally {
                     unset($shop);
                }
                
            }
            else {
                $view = $this->getActionView();
                $view->set("errors",$shop->getErrors());
                $this->render();
            }
        }
        else {
            $this->render();
        }
    }
}
